package zhcms.framework;

public class GyReplace {
	
	private String tempS;
	
	public GyReplace() {
		this.tempS="";
	}
	public String replace(String strSource, String strFrom, String strTo) {    
	     if (strSource == null) {        
	       return null;    
	     }  
	     int i = 0;
	     if ((i = strSource.indexOf(strFrom, i)) >= 0) {
	       char[] cSrc = strSource.toCharArray();
	       char[] cTo = strTo.toCharArray();
	       int len = strFrom.length();  
	       StringBuffer buf = new StringBuffer(cSrc.length);  
	       buf.append(cSrc, 0, i).append(cTo);
	       i += len;
	       int j = i;
	       while ((i = strSource.indexOf(strFrom, i)) > 0) {  
	         buf.append(cSrc, j, i - j).append(cTo);   
	         i += len;  
	         j = i;        
	       }        
	       buf.append(cSrc, j, cSrc.length - j);
	       return buf.toString();
	     }
	     return strSource;
	   }
	
	public void replaceAll(String strSource,String StrFrom,String strTo)
	{
		//System.out.println("源的字符串"+strSource);
		//System.out.println("要替换的字符串"+StrFrom);
		//System.out.println("替换后的字符串"+this.tempS);
		if(strSource.indexOf(StrFrom)>0)
		{
			GySplit a=new GySplit();
			a.tool(strSource.toString(), StrFrom.toString());
			String[] temp_array=a.Show();
			a=null;
			this.tempS=temp_array[0]+strTo+ temp_array[1];
			replaceAll(this.tempS,StrFrom,strTo);
		}
		else
		{
			
		}
		//return strSource;
	}
	
	public String replaceAllNew1(String strSource,String StrFrom,String strTo)
	{
		//System.out.println("源的字符串"+strSource);
		//System.out.println("要替换的字符串"+StrFrom);
		//System.out.println("替换后的字符串"+this.tempS);
		int i = StrFrom.length();
		   if (i==0)
			   this.tempS=strSource;
		   String s3;
		   for (int j = strSource.indexOf(StrFrom); j != -1; j = (strSource = s3 + strTo + strSource.substring(j + i)).indexOf(StrFrom))
		      s3 = new String(strSource.substring(0, j));

		 return strSource; 
	}
	
	public void replaceAllNew(String strSource,String StrFrom,String strTo)
	{
		//System.out.println("源的字符串"+strSource);
		//System.out.println("要替换的字符串"+StrFrom);
		//System.out.println("替换后的字符串"+strTo);
		int i = StrFrom.length();
   if (i==0)
	   this.tempS=strSource;
   String s3;
   for (int j = strSource.indexOf(StrFrom); j != -1; j = (strSource = s3 + strTo + strSource.substring(j + i)).indexOf(StrFrom))
      s3 = new String(strSource.substring(0, j));

   this.tempS=strSource;
		//return strSource;
	}
	
	public String Show()
	{
		return this.tempS;
	}
}
